package com.waimai1.waimai.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.waimai1.waimai.common.BaseContext;
import com.waimai1.waimai.common.R;
import com.waimai1.waimai.common.Utils;
import com.waimai1.waimai.dto.GetOrderListDto;
import com.waimai1.waimai.entity.MyOrder;
import com.waimai1.waimai.entity.User;
import com.waimai1.waimai.exception.EnumException;
import com.waimai1.waimai.exception.NotErrorException;
import com.waimai1.waimai.service.HwService;
import com.waimai1.waimai.service.MyOrderService;
import com.waimai1.waimai.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.javassist.bytecode.LineNumberAttribute;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.text.ParseException;
import java.util.*;

@Slf4j
@RestController
@RequestMapping("/order")
@ResponseBody
public class orderController {
    @Autowired
    private MyOrderService orderService;
    @Autowired
    private UserService userService;
    @Autowired
    private HwService hwService;
    @PostMapping("/getOrderList")
    public R<List<MyOrder>> getOrderList(@RequestBody GetOrderListDto getOrderListDto){
        List<Integer> statusList=new ArrayList<>();
        switch (getOrderListDto.getStatusTab()){
            case 1:
                statusList.add(Utils.STATUS_WAIT_PAY);
                break;
            case 2:
                statusList.add(Utils.STATUS_PAYED);
                statusList.add(Utils.STATUS_WAIT_REFUND);
                break;
            case 3:
                statusList.add(Utils.STATUS_OVER);
                break;
            case 4:
                statusList.add(Utils.STATUS_CANCEL);
                statusList.add(Utils.STATUS_REFUNDED);
                break;
        }
        LambdaQueryWrapper<MyOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MyOrder::getOpenid, BaseContext.getopenId())
                .eq(MyOrder::getAcid,BaseContext.getAcid())
        .in(getOrderListDto.getStatusTab()!=0, MyOrder::getStatus,statusList)
        .orderByDesc(MyOrder::getUpdateTime);
        Page<MyOrder> page=new Page<>(getOrderListDto.getPageIndex(),10);
        List<MyOrder> orderList= orderService.page(page,wrapper).getRecords();
        return R.success(orderList);
    }
    //    卖出的订单
    @PostMapping("/getSellOrderList")
    public R<List<MyOrder>> getSellOrderList(@RequestBody GetOrderListDto getOrderListDto){
        List<Integer> statusList=new ArrayList<>();
        switch (getOrderListDto.getStatusTab()){
            case 1:
                statusList.add(Utils.STATUS_WAIT_PAY);
                break;
            case 2:
                statusList.add(Utils.STATUS_PAYED);
                statusList.add(Utils.STATUS_WAIT_REFUND);
                break;
            case 3:
                statusList.add(Utils.STATUS_OVER);
                break;
            case 4:
                statusList.add(Utils.STATUS_CANCEL);
                statusList.add(Utils.STATUS_REFUNDED);
                break;
        }
        LambdaQueryWrapper<MyOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MyOrder::getLocalShopId,getOrderListDto.getLocalShopId())
                .in(getOrderListDto.getStatusTab()!=0, MyOrder::getStatus,statusList)
                .orderByDesc(MyOrder::getUpdateTime);
        Page<MyOrder> page=new Page<>(getOrderListDto.getPageIndex(),10);
        List<MyOrder> orderList= orderService.page(page,wrapper).getRecords();
        return R.success(orderList);
    }
    @PostMapping("/getTuanOrderList")
    public R<List<MyOrder>> getTuanOrderList(@RequestBody GetOrderListDto getOrderListDto){
        List<Integer> statusList=new ArrayList<>();
        switch (getOrderListDto.getStatusTab()){
            case 1:
                statusList.add(Utils.STATUS_WAIT_PAY);
                break;
            case 2:
                statusList.add(Utils.STATUS_PAYED);
                statusList.add(Utils.STATUS_WAIT_REFUND);
                break;
            case 3:
                statusList.add(Utils.STATUS_OVER);
                break;
            case 4:
                statusList.add(Utils.STATUS_CANCEL);
                statusList.add(Utils.STATUS_REFUNDED);
                break;
        }
        LambdaQueryWrapper<MyOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MyOrder::getAcid,BaseContext.getAcid())
                .eq(MyOrder::getTuanId, getOrderListDto.getTuanId())
                .in(getOrderListDto.getStatusTab()!=0, MyOrder::getStatus,statusList)
                .orderByDesc(MyOrder::getUpdateTime);
        Page<MyOrder> page=new Page<>(getOrderListDto.getPageIndex(),10);
        List<MyOrder> orderList= orderService.page(page,wrapper).getRecords();
        return R.success(orderList);
    }
    @GetMapping("/getPintuanStatus/{orderId}")
    public R<Object> getPintuanStatus(@PathVariable BigInteger orderId){
        MyOrder order=orderService.getById(orderId);
        List<Object> goods= JSON.parseArray(order.getComments());
        Map<String,Object> good=(Map<String,Object>)goods.get(0);
        Integer pinCount=(Integer) good.get("pinCount");
        //获取拼团人数
        LambdaQueryWrapper<MyOrder> orderLambdaQueryWrapper=new LambdaQueryWrapper<>();
        orderLambdaQueryWrapper.eq(MyOrder::getRemark,"拼团")
                .eq(MyOrder::getAcid,BaseContext.getAcid())
                .in(MyOrder::getStatus, new Integer[]{Utils.STATUS_PAYED, Utils.STATUS_OVER})
                .gt(MyOrder::getCreateTime,order.getCreateTime())
                .orderByAsc(MyOrder::getCreateTime);
        Page<MyOrder> page=new Page<>(1,pinCount-1);
        List<MyOrder> orderList= orderService.page(page,orderLambdaQueryWrapper).getRecords();
//        根据openid获取参加拼团人的头像
        List<String> openidList=new ArrayList<>();
        openidList.add(order.getOpenid());
        if(orderList.size()!=0){
            for(MyOrder resOrder : orderList ){
                openidList.add(resOrder.getOpenid());
            }
        }
        LambdaQueryWrapper<User> userLambdaQueryWrapper=new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getAcid,BaseContext.getAcid())
                .in(User::getOpenid,openidList);
        List<User> users=userService.list(userLambdaQueryWrapper);
        List<String> imgs=new ArrayList<>();
        for (User user:users){
            imgs.add(user.getImage());
        }
        return R.success(imgs);
    }
    @PostMapping("/getOrder")
    public R<MyOrder> getOrder(@RequestBody String outTradeNo){
        LambdaQueryWrapper<MyOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MyOrder::getAcid,BaseContext.getAcid()).eq(MyOrder::getOutTradeNo,outTradeNo);
        MyOrder order=orderService.getOne(wrapper);
        if(order==null){
            throw new NotErrorException(EnumException.NO_ORDER);
        }
        return R.success(order);
    }
    @PostMapping ("/hwNotify")
    public String notify(@RequestBody Map<String,Object> map) throws NoSuchFieldException, InstantiationException, IllegalAccessException, JsonProcessingException, ParseException {
        return  hwService.hwNotify(map);
    }
    @PostMapping("/hwSearchOrder")
    public void searchOrder(@RequestBody String orderNo) throws JsonProcessingException, InstantiationException, IllegalAccessException, NoSuchFieldException, ParseException {
        hwService.searchOrder(orderNo);
    }
}
